import UserAgent from 'user-agents';

export const genEnvInfo = (formData:any) =>{
    let newData = {
        envName: formData.envName,
        os: formData.os,
        browser: formData.browser,
        userAgent: formData.userAgent,
        deviceName: formData.deviceName,
        timeZone: formData.timeZone,
        webRTC: formData.webRTC,
        location: formData.location,
        language: formData.language,
        resolution: formData.resolution,
        canvas: formData.canvas,
        webgl: formData.webgl,
        webGLCorp: formData.webGLCorp,
        webGLRender: formData.webGLRender,
        audioContext: formData.audioContext,
        mediaDevice: formData.mediaDevice,
        concurrent: formData.concurrent,
        mem: formData.mem,
        speechVoice: formData.speechVoice,
        browserVersion: formData.browserVersion
    };

    let restrict:any = [
    ];

    if(newData.os.indexOf("Mac")>=0){
        restrict.push({
            platform: "MacIntel"
        })
        restrict.push({
            deviceCategory: 'desktop'
        })
        restrict.push(/Chrome/);
    }else if(newData.os.indexOf("Windows")>=0){
        restrict.push({
            platform: "Win32"
        })
        restrict.push({
            deviceCategory: 'desktop'
        })
        restrict.push(/Chrome/);
    }else if(newData.os.indexOf("Linux")>=0){
        restrict.push({
            platform: "Linux x86_64"
        })
        restrict.push({
            deviceCategory: 'desktop'
        })
        restrict.push(/Chrome/);
    }else if(newData.os.indexOf("Android")>=0){
        restrict.push({
            platform: "Linux armv8l"
        })
        restrict.push({
            deviceCategory: 'mobile'
        })
        restrict.push(/Chrome/);
    }else if(newData.os.indexOf("iOS")>=0){
        restrict.push({
            platform: "iPhone"
        })
        restrict.push({
            deviceCategory: 'mobile'
        })
        restrict.push(/Safari/);
    }
    
    let userAgent = new UserAgent(restrict).toString();

    // 替换Chrome版本
    if(newData.browserVersion!='随机'){
        let UAArrary = userAgent.split(' ').filter((item:any)=>item.indexOf('Chrome/')>=0);
        if(UAArrary && UAArrary.length>0){
            let str = UAArrary[0];
            userAgent = userAgent.replace(str, "Chrome/"+newData.browserVersion);
        }
    }

    newData.userAgent = userAgent;
    console.log('genEnvInfo', newData);


    return newData;
}